package com.heart.house.web.controller;

import com.heart.house.biz.service.*;
import com.heart.house.common.constants.CommonConstants;
import com.heart.house.common.model.*;
import com.heart.house.common.page.PageData;
import com.heart.house.common.page.PageParams;
import com.heart.house.common.result.ResultMsg;
import com.heart.house.web.interceptor.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author Luo Zhen
 * @create 2019-06-24 10:29
 */
@Controller
public class HouseController {

    @Autowired
    private HouseService houseService;
    @Autowired
    private CityService cityService;
    @Autowired
    private AgencyService agencyService;
    @Autowired
    private RecommandService recommandService;
    @Autowired
    private CommentService commentService;

    /**
     * 房产列表`
     * 1.分页 搜索 类型搜索
     * 2.支持排序
     * 3.支持展示图片 价格 标题 地址
     *
     * @return
     */
    @RequestMapping("/house/list")
    public String houseList(Integer pageSize, Integer pageNum, House query, ModelMap modelMap) {
        PageData<House> ps = houseService.queryHouse(query, PageParams.build(pageSize, pageNum));
        List<House> hotHouses = recommandService.getHostList(CommonConstants.RECOM_SIZE);
        modelMap.put("recomHouses", hotHouses);
        modelMap.put("ps", ps);
        modelMap.put("vo", query);
        return "house/listing";
    }

    /**
     * 房屋添加页面
     *
     * @param modelMap
     * @return
     */
    @RequestMapping("/house/toAdd")
    public String toAdd(ModelMap modelMap) {
        modelMap.put("citys", cityService.findAll());
        modelMap.put("communitys", houseService.findAllCommunitys());
        return "house/add";
    }

    /**
     * 1.获取用户
     * 2.设置房产状态
     * 3.添加房产
     *
     * @param house
     * @return
     */
    @RequestMapping("/house/add")
    public String add(House house) {
        User user = UserContext.getUser();
        house.setState(CommonConstants.HOUSE_STATE_UP);
        houseService.addHouse(house, user);
        return "redirect:/house/ownlist";
    }

    @RequestMapping("/house/ownlist")
    public String ownlist(House house, Integer pageSize, Integer pageNum, ModelMap modelMap) {
        User user = UserContext.getUser();
        house.setUserId(user.getId());
        house.setBookmarked(false);
        modelMap.put("ps", houseService.queryHouse(house, PageParams.build(pageSize, pageNum)));
        modelMap.put("pageType", "own");
        return "house/ownlist";
    }

    /**
     * 查询房屋详情
     * 查询房屋关联经纪人
     * s
     *
     * @param id
     * @return
     */
    @RequestMapping("/house/detail")
    public String houseDetail(Long id, ModelMap modelMap) {
        House house = houseService.queryOneHouse(id);
        // 防止模拟用户详情请求跳转到主页
        if (house == null) {
            return "redirect:list";
        }
        HouseUser houseUser = houseService.getHouseUser(id);
        recommandService.increase(id);
        if (houseUser != null && houseUser.getUserId() != null && !houseUser.getUserId().equals(0)) {
            modelMap.put("agent", agencyService.getAgentDetail(houseUser.getUserId()));
        }
        List<House> hotHouses = recommandService.getHostList(CommonConstants.RECOM_SIZE);
        modelMap.put("recomHouses", hotHouses);
        modelMap.put("house", house);
        //todo 评论信息
        List<Comment> commentList = commentService.getHouseComments(id);
        modelMap.put("commentList", commentList);
        return "house/detail";
    }

    /**
     * 发送邮箱至房屋经纪人
     *
     * @param userMsg
     * @return
     */
    @RequestMapping("/house/leaveMsg")
    public String houseMsg(UserMsg userMsg) {
        houseService.addUserMsg(userMsg);
        return "redirect:/house/detail?id=" + userMsg.getHouseId();
    }

    /**
     * 1.评分
     *
     * @param id
     * @param rating
     * @return
     */
    @RequestMapping("/house/rating")
    @ResponseBody
    public ResultMsg rating(Long id, Double rating) {
        houseService.updateRating(id, rating);
        return ResultMsg.successMsg("ok");
    }

    /**
     * 2.收藏
     *
     * @return
     */
    @RequestMapping("/house/bookmark")
    @ResponseBody
    public ResultMsg bookmark(Long id) {
        User user = UserContext.getUser();
        houseService.bindUser2House(id, user.getId(), true);
        return ResultMsg.successMsg("ok");
    }

    /**
     * 3.删除收藏
     *
     * @param id
     * @return
     */
    @RequestMapping("/house/unbookmark")
    @ResponseBody
    public ResultMsg unbookmark(Long id) {
        User user = UserContext.getUser();
        houseService.unbindUser2House(id, user.getId(), HouseUserType.BOOKMARK);
        return ResultMsg.successMsg("ok");
    }

    /**
     * 4.收藏列表
     *
     * @param house
     * @param pageSize
     * @param pageNum
     * @param modelMap
     * @return
     */
    @RequestMapping("/house/bookmarked")
    public String bookmarked(House house, Integer pageSize, Integer pageNum, ModelMap modelMap) {
        User user = UserContext.getUser();
        house.setBookmarked(true);
        house.setUserId(user.getId());
        modelMap.put("ps", houseService.queryHouse(house, PageParams.build(pageSize, pageNum)));
        modelMap.put("pageType", "book");
        return "house/ownlist";
    }

    /**
     * 5.删除房产资源
     *
     * @param id
     * @param pageType
     * @return
     */
    @RequestMapping(value = "/house/del")
    public String delsale(Long id, String pageType) {
        User user = UserContext.getUser();
        houseService.unbindUser2House(id, user.getId(), pageType.equals("own") ? HouseUserType.SALE : HouseUserType.BOOKMARK);
        return "redirect:/house/ownlist";
    }


}
